package com.df.bpm.utils.businessViewer;

import com.df.bpm.entity.BusinessInformation;
import com.df.common.utils.CommonUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 主界面板
 * <P>1.展示业务路径</P>
 * <P>2.展示业务内容（步骤）</P>
 */
public class MainPanel extends JPanel {
    Dimension maxSize=null;
    BusinessPathTable businessPathTable=new BusinessPathTable();
    BusinessStepsTable businessStepsTable;

    BusinessInformation focusBusiness;

    JButton showInputs=new JButton("输入清单");
    JButton showOutputs=new JButton("输出清单");

    public static InputsDialog inputsDialog;
    public static OutputsDialog outputsDialog;

    /**
     * 长度单位
     */
    public static double unit=10;
    public MainPanel() {
        businessStepsTable=new BusinessStepsTable(this);
        getScreenMaxSize();
        this.setLayout(null);
        businessPathTable.setBounds((int) unit, (int) unit, (int) (15*unit), (int) (maxSize.getHeight()-4*unit));
        this.add(businessPathTable);

        showInputs.setBounds(
                businessPathTable.getX()+businessPathTable.getWidth()+(int) unit,
                (int) unit,
                (int) (5*unit),
                (int) (2*unit)
        );
        this.add(showInputs);
        showInputs.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                inputsDialog.changeVisible();
                showInputs();
            }
        });
        showOutputs.setBounds(
                showInputs.getX()+showInputs.getWidth()+(int) unit,
                showInputs.getY(),
                showInputs.getWidth(),
                showInputs.getHeight()
        );
        this.add(showOutputs);
        showOutputs.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                outputsDialog.changeVisible();
                showOutputs();
            }
        });
        businessStepsTable.setLocation(
                showInputs.getX(),
                (int) (showInputs.getY()+showInputs.getHeight()+unit)
        );
        businessStepsTable.setSize(
                (int) (maxSize.getWidth()-(businessPathTable.getX()+businessPathTable.getWidth())-3*unit),
                (int) (maxSize.getHeight()-businessStepsTable.getY()-3*unit)
        );
        this.add(businessStepsTable);

        freshBusinessPathTable();
        goToBusinessPath(0);
    }

    private void showInputs() {
        if(CommonUtils.varIsBlank(inputsDialog)){
            inputsDialog=new InputsDialog();
        }
        inputsDialog.show(this.focusBusiness);
    }
    private void showOutputs() {
        if(CommonUtils.varIsBlank(outputsDialog)){
            outputsDialog=new OutputsDialog();
        }
        outputsDialog.show(this.focusBusiness);
    }

    public void freshBusinessSteps(BusinessInformation businessInformation){
        businessStepsTable.fresh(businessInformation);
        for(JTable iTable:businessStepsTable.dataTables){
            iTable.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int row = iTable.rowAtPoint(e.getPoint());
                    int col = iTable.columnAtPoint(e.getPoint());
                    if (row >= 0) {
                        if (e.getClickCount() == 2) {//双击跳转到目标业务
                            if(col==1){
                                if(CommonUtils.varIsNotBlank(iTable.getValueAt(row, 0))){
                                    Integer groupID= (Integer) iTable.getValueAt(row, 5);
                                    BusinessInformation iStep=businessStepsTable.group_steps.get(groupID).get(row+1).getBusiness();
                                    businessPathTable.addPath(iStep);
                                    freshBusinessPathTable();
                                    freshBusinessSteps(iStep);
                                }
                            }

                        }
                    }
                }
            });
        }
        setFocusBusiness(businessInformation);
    }

    private void freshBusinessPathTable() {
        businessPathTable.fresh();
        businessPathTable.dataTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = businessPathTable.dataTable.rowAtPoint(e.getPoint());
                if (row >= 0) {
                    if (e.getClickCount() == 2) {//双击跳转到目标业务
                        goToBusinessPath(row);
                    }
                }
            }
        });
    }

    /**
     * 跳转路径
     * @param row 路径行号
     */
    private void goToBusinessPath(int row) {
        freshBusinessSteps(businessPathTable.pathBusiness.get(row));
        businessPathTable.toPath(row);
        freshBusinessPathTable();
    }

    public void setFocusBusiness(BusinessInformation businessInformation) {
        this.focusBusiness=businessInformation;
        showInputs();
        showOutputs();
    }


    /**
     * 获取屏幕尺寸（不包含任务栏）
     */
    private void getScreenMaxSize(){
        JFrame jf=new JFrame();
        jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
        jf.setVisible(true);
        jf.setVisible(false);
        maxSize=jf.getSize();
        jf.dispose();
        double iw=maxSize.getWidth();//屏幕宽
        double ih=maxSize.getHeight();//屏幕高
        unit=Math.sqrt(iw*iw+ih*ih)/100;//用屏幕对角线尺寸的百分之一当作长度单位
    }
}
